package com.wisewin.circle.api.listener;

import com.wisewin.circle.util.IpUtil;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@WebListener
public class MyServletListener implements ServletRequestListener {

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        HttpSession session = request.getSession();
        System.out.println("requestInitialized session = " + session);
        //把HttpServletRequest中的IP地址放入HttpSession中，关键字可任取，此处为ClientIP
        IpUtil ipUtil = new IpUtil();
        session.setAttribute("ClientIP", ipUtil.getIpAddr(request));
        System.out.println("request.getRemoteAddr：" + ipUtil.getIpAddr(request));
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("requestDestroyed sre = " + sre);
    }

}
